//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.Authentication
{
    using System;
    using System.Collections.ObjectModel;
    using System.DirectoryServices;
    using System.Security.Permissions;

    public abstract class BaseAuthentication
    {
        internal const string NAME = "name";

        internal const string DCPREFIX = "DC=";

        #region IAuthentication Members

        [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        public abstract ICredentials GetUser(ICredentials credentials);

        [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        public abstract ICredentials GetGroup(ICredentials credentials);

        [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        public abstract Collection<string> GetGroups(ICredentials credentials);

        /// <summary>
        /// Get the Domain Name
        /// </summary>
        /// <param name="distinguishedName">string</param>
        /// <returns>DomainName</returns>
        internal static string GetDomainFromDistinguishedName(string distinguishedName)
        {
            string domainName;
            // get the Start Index of DC=
            int startIndex = distinguishedName.IndexOf(DCPREFIX, StringComparison.OrdinalIgnoreCase) + 3;
            // Truncate the strings before DC=
            distinguishedName = distinguishedName.Substring(startIndex);
            // Get the DC Value from the Truncated string
            domainName = distinguishedName.Substring(0, distinguishedName.IndexOf(','));

            return domainName;
        }

        /// <summary>
        /// GetPropertyValue for the property Name
        /// </summary>
        /// <param name="searchEntry">DirectoryEntry</param>
        /// <param name="property">string</param>
        /// <returns>property Value</returns>
        [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        internal static string GetPropertyValue(DirectoryEntry searchEntry, string property)
        {
            string value = string.Empty;
            if (searchEntry.Properties[property].Value != null)
            {
                value = searchEntry.Properties[property].Value.ToString();
            }

            return value;
        }

        #endregion
    }

    public interface ICredentials
    {
        /// <summary>
        /// Get and Set Name
        /// </summary>
        string Name
        {
            get;
            set;
        }

        /// <summary>
        /// Get and Set UserName
        /// </summary>
        string UserName
        {
            get;
            set;

        }

        /// <summary>
        /// Get and Set Domain Name
        /// </summary>
        string DomainName
        {
            get;
            set;

        }

        /// <summary>
        /// Get and Set DisplayName
        /// </summary>
        string DisplayName
        {
            get;
            set;
        }

        /// <summary>
        /// Get and Set CredentialType
        /// </summary>
        CredentialType CredentialType
        {
            get;
            set;
        }
    }


    /// <summary>
    /// enum CredentialType
    /// </summary>
    public enum CredentialType
    {
        User,
        Group
    }



}
